const idsDao = require('./idsDao')
const db = require('./db')

class UserDao {

    newAndSave(username, password, cb) {
        let self = this
        idsDao.findOneAndUpdate('userId', function (err, numAffected, affectedDocuments) {
            if (err || !affectedDocuments)
                throw err
            let id = affectedDocuments.userId * 1000 + 1 //id后面3位是服务器id
            db.users.insert({
                _id: id,
                username: username,
                password: password,
                registerTime: new Date().getTime()
            }, cb);
        })
    }

    findOne(query, cb) {
        db.users.findOne(query, function (err, doc) {
            if (err) {
                console.error(err);
            } else {
                cb(doc);
            }
        })
    }
}
let userDao = new UserDao();
module.exports = userDao;